/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ryopei.hatena;

import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.swingx.JXImagePanel;

/**
 *
 * @author ryohei
 */
public class BooleanCellRenderer extends DefaultTableCellRenderer {

    private Image checkImage;

    public BooleanCellRenderer() {
        ResourceMap resMap = Application.getInstance(HatenaCategoryManagerApp.class).getContext().getResourceMap(HatenaCategoryManagerView.class);
        checkImage = resMap.getImageIcon("accept.icon").getImage();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        int modelColumn = table.getColumnModel().getColumn(column).getModelIndex();
        
        Boolean b = (Boolean) value;
        Color background = null;
        Component comp;
        SimpleEntryModel.RowDataModel m = (SimpleEntryModel.RowDataModel) table.getModel();
        SimpleEntry e = m.getEntry(row);

        if (b) {
            JXImagePanel p = new JXImagePanel();
            p.setImage(checkImage);
            comp = p;
            if (!e.containsInitialCategories(m.getColumnName(modelColumn))) {
                background = Color.YELLOW;
            }
        } else {
            comp = super.getTableCellRendererComponent(table, "", isSelected, hasFocus, row, modelColumn);
            if (e.containsInitialCategories(m.getColumnName(modelColumn))) {
                background = Color.YELLOW;
            }
        }

        if (isSelected) {
            comp.setBackground(super.getTableCellRendererComponent(table, "", isSelected, hasFocus, row, modelColumn).getBackground());
        } else {
            comp.setBackground(background);
        }

        return comp;
    }
}
